<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
		<meta http-equiv="Content-Style-Type" content="text/css">
		<title>PikaScript Library Reference: utils</title>
	</head>
	<body>
		<div class="navigation"><a href="index.html">toc</a></div>
		<hr>
		<h2>utils</h2>
			<p><a href="utils.html#VERSION">VERSION</a>, <a href="utils.html#args">args</a>, <a href="utils.html#classify">classify</a>, <a href="utils.html#coalesce">coalesce</a>, <a href="utils.html#compare">compare</a>, <a href="utils.html#defaults">defaults</a>, <a href="utils.html#delete">delete</a>, <a href="utils.html#evaluate">evaluate</a>, <a href="utils.html#exists">exists</a>, <a href="utils.html#include">include</a>, <a href="utils.html#input">input</a>, <a href="utils.html#invoke">invoke</a>, <a href="utils.html#load">load</a>, <a href="utils.html#max">max</a>, <a href="utils.html#min">min</a>, <a href="utils.html#parse">parse</a>, <a href="utils.html#print">print</a>, <a href="utils.html#run">run</a>, <a href="utils.html#save">save</a>, <a href="utils.html#sourceFor">sourceFor</a>, <a href="utils.html#swap">swap</a>, <a href="utils.html#system">system</a>, <a href="utils.html#throw">throw</a>, <a href="utils.html#time">time</a>, <a href="utils.html#toSource">toSource</a>, <a href="utils.html#try">try</a>, <a href="utils.html#vargs">vargs</a></p>
		<hr>
			
<div>
	<h3><a name="VERSION">VERSION</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'v' = VERSION</pre>
	<h4>Description</h4>
	<div class="description"><p>VERSION is a global variable containing the PikaScript engine version as a human readable text string.</p></div>
	
	
</div>
<hr>

<div>
	<h3><a name="args">args</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">args([@variables, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>Assigns arguments to named variables. Pass a reference to a local variable for each argument your function expects. The caller of your function must pass exactly this number of arguments or an exception will be thrown.</p></div>
	<h4>Examples</h4><pre class="examples">args(@x, @y)</pre>
	<h4>See Also</h4><p class="seealso"><a href="#vargs">vargs</a></p>
</div>
<hr>

<div>
	<h3><a name="classify">classify</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'class' = classify(&lt;value&gt;)</pre>
	<h4>Description</h4>
	<div class="description"><p>Examines &lt;value&gt; and tries to determine what "value class" it belongs to:</p><p>- 'void' (empty string)<br>- 'boolean' ('true' or 'false')<br>- 'number' (starts with a digit, '+' or '-' and is convertible to a number)<br>- 'reference' (starting with ':' and containing at least one more ':')<br>- 'function' (enclosed in '{ }' or begins with '&gt;:' and contains one more ':')<br>- 'native' (enclosed in '&lt; &gt;')<br>- 'string' (if no other match)</p><p>Notice that since there are no strict value types in PikaScript the result of this function should be considered a "hint" only of what type &lt;value&gt; is. For example, there is no guarantee that a value classified as a 'function' actually contains executable code.</p></div>
	<h4>Examples</h4><pre class="examples">classify(void) === 'void'<br>classify('false') === 'boolean'<br>classify(123.456) === 'number'<br>classify(@localvar) === 'reference'<br>classify(function { }) === 'function'<br>classify(&gt;lambda) === 'function'<br>classify('&lt;print&gt;') === 'native'<br>classify('tumbleweed') === 'string'</pre>
	
</div>
<hr>

<div>
	<h3><a name="coalesce">coalesce</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;value&gt; = coalesce(&lt;values&gt;|@variables, ...)</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns the first &lt;value&gt; in the argument list that is non-void, or the contents of the first @variables that exists (whichever comes first). void is returned if everything else fails.</p><p>A word of warning here, if a string value happens to look like a reference (e.g. '::') it will be interpreted as a such which may yield unexpected results. E.g. coalesce('::uhuh', 'oops') will not return 'oops' if a global variable named 'uhuh' exists.</p></div>
	<h4>Examples</h4><pre class="examples">coalesce(@gimmeVoidIfNotDefined)<br>coalesce(maybeVoid, perhapsThisAintVoid, 'nowIAmDefinitelyNotVoid')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#defaults">defaults</a>, <a href="#exists">exists</a></p>
</div>
<hr>

<div>
	<h3><a name="compare">compare</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;diff&gt; = compare(&lt;a&gt;, &lt;b&gt;)</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns 0 if &lt;a&gt; equals &lt;b&gt;, -1 if &lt;a&gt; is less than &lt;b&gt; and 1 if &lt;a&gt; is greater than &lt;b&gt;. This function is useful in sorting algorithms.</p></div>
	<h4>Examples</h4><pre class="examples">compare('abc', 'def') &lt; 0<br>compare('def', 'abc') &gt; 0<br>compare('abc', 'abc') == 0</pre>
	<h4>See Also</h4><p class="seealso"><a href="arrays.html#qsort">qsort</a>, <a href="#swap">swap</a></p>
</div>
<hr>

<div>
	<h3><a name="defaults">defaults</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">defaults([@variable, &lt;value&gt;, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>Assigns each &lt;value&gt; to each @variable if it does not exist. This function is useful for initializing global variables and optional function arguments.</p></div>
	<h4>Examples</h4><pre class="examples">defaults(@name, 'Smith')<br>defaults(@first, 'a', @last, 'z')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#coalesce">coalesce</a></p>
</div>
<hr>

<div>
	<h3><a name="delete">&lt;delete&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">?deleted = delete(@variable)</pre>
	<h4>Description</h4>
	<div class="description"><p>Deletes the variable referenced to by @variable and returns true if the variable existed and was successfully deleted.</p><p>Notice that this function can only delete a single variable at a time. This means only a single "element" in a "container" as well. Use prune() to delete an entire "plain old data" container and destruct() to delete an object.</p></div>
	<h4>Examples</h4><pre class="examples">delete(@begone)<br>delete(@hurray[1972])</pre>
	<h4>See Also</h4><p class="seealso"><a href="objects.html#destruct">destruct</a>, <a href="#exists">exists</a>, <a href="containers.html#prune">prune</a></p>
</div>
<hr>

<div>
	<h3><a name="evaluate">&lt;evaluate&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;result&gt; = evaluate('code', [@frame])</pre>
	<h4>Description</h4>
	<div class="description"><p>Evaluates 'code' and returns the result. You can decide which frame should execute the code by supplying a reference in @frame. Without @frame, code is executed in its own frame just as if you would call a function. Only the "frame identifier" of @frame is used.</p><p>You may for example pass @$ to execute in the current frame, or @^$ to execute in the caller's frame (the '$' is there to reference the correct frame even if it is a lambda expression), or @:: to execute in the global frame.</p></div>
	<h4>Examples</h4><pre class="examples">evaluate('3 + 3') == 6<br>evaluate('x = random(1)', @x)</pre>
	<h4>See Also</h4><p class="seealso"><a href="strings.html#bake">bake</a>, <a href="#invoke">invoke</a>, <a href="#parse">parse</a>, <a href="#run">run</a>, <a href="#sourceFor">sourceFor</a>, <a href="#toSource">toSource</a></p>
</div>
<hr>

<div>
	<h3><a name="exists">&lt;exists&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">?found = exists(@variable)</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns true if the variable referenced to by @variable is defined. Notice that this function does not "fall back" to the global frame if a local variable does not exist. Therefore you should always prefix the variable name with '::' if you want to check for the existance of a global variable or function.</p></div>
	<h4>Examples</h4><pre class="examples">exists(@::aglobal)<br>exists(@users['magnus lidstrom'])</pre>
	<h4>See Also</h4><p class="seealso"><a href="#coalesce">coalesce</a>, <a href="#defaults">defaults</a>, <a href="#delete">delete</a></p>
</div>
<hr>

<div>
	<h3><a name="include">include</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">include('filePath')</pre>
	<h4>Description</h4>
	<div class="description"><p>Runs a PikaScript source file as with run() but only if 'filePath' has not been "included" before. The function determines this by checking the existance of a global ::included[filePath]. It defines this global after completing execution the first time. (run() does not define or use this global.) Use include() with source files that defines library functions and constants, e.g. 'stdlib.pika'.</p></div>
	<h4>Examples</h4><pre class="examples">include('stdlib.pika')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#run">run</a></p>
</div>
<hr>

<div>
	<h3><a name="input">&lt;input&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'answer' = input('question')</pre>
	<h4>Description</h4>
	<div class="description"><p>Prints 'question' and returns a line read from the standard input stream (excluding any terminating line feed characters). May throw 'Unexpected end of input file' or 'Input file error'.</p></div>
	<h4>Examples</h4><pre class="examples">name = input("What's your name? ")</pre>
	<h4>See Also</h4><p class="seealso"><a href="#print">print</a></p>
</div>
<hr>

<div>
	<h3><a name="invoke">&lt;invoke&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;result&gt; = invoke(['callee'], [&gt;body], @args, [+offset = 0], [+count])</pre>
	<h4>Description</h4>
	<div class="description"><p>Calls 'callee' (or &gt;body) with the argument list @args. The difference between using 'callee' or &gt;body is that the former should be a string with a function name, while the latter should be an actual function body. If both arguments are present, &gt;body will be executed, but the called function's $callee variable will be set to 'callee'. For debugging purposes it is recommended that you use the 'callee' argument.</p><p>+offset can be used to adjust the element index for the first argument. +count is the number of arguments. If it is omitted, [@args].n is used to determine the count.</p><p>May throw 'Too few array elements'.</p></div>
	<h4>Examples</h4><pre class="examples">invoke('max', , @values)<br>invoke('(callback)', $0, @$, 1, 4)</pre>
	<h4>See Also</h4><p class="seealso"><a href="#evaluate">evaluate</a>, <a href="objects.html#invokeMethod">invokeMethod</a>, <a href="#run">run</a></p>
</div>
<hr>

<div>
	<h3><a name="load">&lt;load&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'contents' = load('filePath')</pre>
	<h4>Description</h4>
	<div class="description"><p>Loads a file from disk and returns it as a string. The standard implementation uses the file I/O of the standard C++ library, which takes care of line ending conversion etc. It can normally only handle ASCII text files. May throw 'Cannot open file for reading: {filePath}' or 'Error reading from file: {filePath}'.</p></div>
	<h4>Examples</h4><pre class="examples">data = load('myfolder/myfile.txt')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#save">save</a></p>
</div>
<hr>

<div>
	<h3><a name="max">max</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;m&gt; = max(&lt;x&gt;, &lt;y&gt;, [&lt;z&gt;, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns the largest value of all the arguments.</p></div>
	<h4>Examples</h4><pre class="examples">max(5, 3, 7, 1, 4) == 7<br>max('Sleepy', 'Grumpy', 'Happy', 'Bashful', 'Dopey', 'Sneezy', 'Doc') === 'Sneezy'<br>max('Zero', '10', '5') === 'Zero'</pre>
	<h4>See Also</h4><p class="seealso"><a href="#min">min</a></p>
</div>
<hr>

<div>
	<h3><a name="min">min</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;m&gt; = min(&lt;x&gt;, &lt;y&gt;, [&lt;z&gt;, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns the smallest value of all the arguments.</p></div>
	<h4>Examples</h4><pre class="examples">min(5, 3, 7, 1, 4) == 1<br>min('Sleepy', 'Grumpy', 'Happy', 'Bashful', 'Dopey', 'Sneezy', 'Doc') === 'Bashful'<br>min('Zero', '10', '5') === '5'</pre>
	<h4>See Also</h4><p class="seealso"><a href="#max">max</a></p>
</div>
<hr>

<div>
	<h3><a name="parse">&lt;parse&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">+offset = parse('code', ?literal)</pre>
	<h4>Description</h4>
	<div class="description"><p>Parses 'code' (without executing it) and returns the number of characters that was successfully parsed. 'code' is expected to start with a PikaScript expression or in case ?literal is true, a single literal (e.g. a number). If ?literal is false, the parsing ends when a semicolon or any unknown or unexpected character is encountered (including unbalanced parentheses etc). The resulting expression needs to be syntactically correct or an exception will be thrown.</p><p>You can use this function to extract PikaScript code (or constants) inlined in other text. You may then evaluate the result with evaluate() and continue processing after the extracted code. Pass true for ?literal in case you want to evaluate a single constant and prevent function calls, assignments etc to be performed. Valid literals are: 'void', 'false', 'true', numbers (incl. hex numbers and 'infinity' of any sign), escaped strings (with single or double quotes), natives (enclosed in '&lt; &gt;'), function or lambda definitions.</p></div>
	<h4>Examples</h4><pre class="examples">parse('3+3', false) == 3<br>parse('3+3', true) == 1<br>parse('1 + 2 * 3 ; stop at semicolon', false) == 10<br>parse(' /* leading comment */ code_here /* skip */ /* trailing */ /* comments */ but stop here', false) == 74<br>parse('stop after space after stop', false) == 5<br>parse('x + x * 3 ) * 7 /* stop at unbalanced ) */', false) == 10<br>parse('+infinity', true) == 9<br>parse('"stop after the \", but before" this text', true) == 31</pre>
	<h4>See Also</h4><p class="seealso"><a href="strings.html#escape">escape</a>, <a href="#evaluate">evaluate</a>, <a href="#run">run</a>, <a href="strings.html#tokenize">tokenize</a></p>
</div>
<hr>

<div>
	<h3><a name="print">&lt;print&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">print('textLine')</pre>
	<h4>Description</h4>
	<div class="description"><p>Prints 'textLine' to the standard output, appending a newline character. (Sorry, but standard PikaScript provides no means for outputting text without the newline.)</p></div>
	<h4>Examples</h4><pre class="examples">print('Hello world!')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#input">input</a></p>
</div>
<hr>

<div>
	<h3><a name="run">run</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">&lt;result&gt; = run('filePath', [&lt;args&gt;, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>Loads and executes a PikaScript source file. The code is executed in the "closure" of the global frame, which means that variable assignments etc work on globals. The code can still use temporary local variables with the $ prefix. The passed &lt;args&gt; are available in $1 and up. $0 will be 'filePath'. The returned value is the final result value of the executed code, just as it would be for a function call. The first line of the source code file may be a "Unix shebang" (in which case it is simply ignored). Use include() if you wish to avoid a file from running more than once.</p></div>
	<h4>Examples</h4><pre class="examples">run('chess.pika')<br>htmlCode = run('makeHTML.pika', 'Make This Text HTML')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#evaluate">evaluate</a>, <a href="#include">include</a>, <a href="#invoke">invoke</a></p>
</div>
<hr>

<div>
	<h3><a name="save">&lt;save&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">save('filePath', 'contents')</pre>
	<h4>Description</h4>
	<div class="description"><p>Saves 'contents' to a file (replacing any existing file). The standard implementation uses the file I/O of the standard C++ library, which takes care of line ending conversion etc. It can normally only handle ASCII text files. May throw 'Cannot open file for writing: {filePath}' or 'Error writing to file: {filePath}'.</p></div>
	<h4>Examples</h4><pre class="examples">save('myfolder/myfile.txt', 'No, sir, away! A papaya war is on!')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#load">load</a></p>
</div>
<hr>

<div>
	<h3><a name="sourceFor">sourceFor</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'code' = sourceFor(@variable|@container, ['prefix' = ''])</pre>
	<h4>Description</h4>
	<div class="description"><p>*** WARNING, THIS FUNCTION IS SLIGHTLY BROKEN AND MAY BE REMOVED. USE AT OWN RISK! ***</p><p>Creates source code for recalling the definition of @variable or @container (with all its sub-elements). The created code can be used with evaluate() to recall the definition in any given frame. For example: evaluate('code', @$) would recreate variables in the current local frame and evaluate('code', @::) would create them in the global frame.</p><p>Each output line will be prefixed with 'prefix'.</p></div>
	<h4>Examples</h4><pre class="examples">save('AllGlobals.pika', sourceFor(@::))<br>evaluate(sourceFor(@wildmatch)) == wildmatch<br>evaluate(sourceFor(@::globalToLocalPlease), @$)<br>print(sourceFor(@myFunction, "\t\t"))</pre>
	<h4>See Also</h4><p class="seealso">dump, <a href="#evaluate">evaluate</a>, <a href="#toSource">toSource</a></p>
</div>
<hr>

<div>
	<h3><a name="swap">swap</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">swap(@a, @b)</pre>
	<h4>Description</h4>
	<div class="description"><p>Swaps the contents of the variables referenced to by @a and @b. This function is useful in sorting algorithms.</p></div>
	<h4>Examples</h4><pre class="examples">swap(@master, @slave)</pre>
	<h4>See Also</h4><p class="seealso"><a href="#compare">compare</a>, <a href="arrays.html#qsort">qsort</a></p>
</div>
<hr>

<div>
	<h3><a name="system">&lt;system&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">+exitCode = system('command')</pre>
	<h4>Description</h4>
	<div class="description"><p>Tries to execute 'command' through the operating system's command interpreter. +exitCode is the return value from the command interpreter (a value of 0 usually means no error). May throw 'Error executing system command: {command}'.</p></div>
	
	
</div>
<hr>

<div>
	<h3><a name="throw">&lt;throw&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">throw('error')</pre>
	<h4>Description</h4>
	<div class="description"><p>Throws an exception. 'error' should describe the error in human readable form. Use try() to catch errors. (PikaScript exceptions are standard C++ exceptions. It is up to the host application how uncaught exceptions are handled.)</p></div>
	<h4>Examples</h4><pre class="examples">throw('Does not compute')</pre>
	<h4>See Also</h4><p class="seealso"><a href="#try">try</a></p>
</div>
<hr>

<div>
	<h3><a name="time">&lt;time&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">+secs = time()</pre>
	<h4>Description</h4>
	<div class="description"><p>Returns the system clock as "epoch time", i.e. the number of seconds that has passed since a specific reference date (usually January 1 1970).</p></div>
	<h4>Examples</h4><pre class="examples">elapsed = time() - lastcheck</pre>
	
</div>
<hr>

<div>
	<h3><a name="toSource">toSource</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'literal' = toSource(&lt;value&gt;)</pre>
	<h4>Description</h4>
	<div class="description"><p>*** WARNING, THIS FUNCTION IS SLIGHTLY BROKEN AND MAY BE REMOVED. USE AT OWN RISK! ***</p><p>Returns the source code literal for &lt;value&gt;. I.e. 'literal' is formatted so that calling evaluate('literal') would bring back the original &lt;value&gt;.</p></div>
	<h4>Examples</h4><pre class="examples">toSource('string theory') === "'string theory'"<br>toSource('') === 'void'<br>toSource('{ /* get funcy */ }') === 'function { /* get funcy */ }'<br>evaluate(toSource(@reffy)) == @reffy<br>evaluate(toSource(&gt;lambchop), @$) == (&gt;lambchop)</pre>
	<h4>See Also</h4><p class="seealso">dump, <a href="#evaluate">evaluate</a>, <a href="#sourceFor">sourceFor</a></p>
</div>
<hr>

<div>
	<h3><a name="try">&lt;try&gt;</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">'exception' = try(&gt;doThis)</pre>
	<h4>Description</h4>
	<div class="description"><p>Executes &gt;doThis, catching any thrown exceptions and returning the error string of the exception. If no exception was caught, void is returned. The returned value of &gt;doThis is discarded. (Although PikaScript exceptions are standard C++ exceptions you can only catch PikaScript errors with this function.)</p></div>
	<h4>Examples</h4><pre class="examples">error = try(&gt;data = load(file))<br>try(&gt;1+1) === void<br>try(&gt;1+'a') === "Invalid number: 'a'"<br>try(&gt;throw('catchme')) === 'catchme'</pre>
	<h4>See Also</h4><p class="seealso"><a href="#throw">throw</a></p>
</div>
<hr>

<div>
	<h3><a name="vargs">vargs</a></h3>
	<h4>Syntax</h4>
	<pre class="syntax">vargs([@arguments, ...], , [@optionals, ...])</pre>
	<h4>Description</h4>
	<div class="description"><p>As args() but you may define arguments that are optional. The caller of your function must pass all required arguments or an exception will be thrown. An exception will also be thrown if the caller passes more optional arguments than present in vargs(). An easy way to assign default values for optional arguments is with the default() function. Alternatively you may want to use coalesce().</p></div>
	<h4>Examples</h4><pre class="examples">vargs(@required1, @required2, , @optional1, @optional2)</pre>
	<h4>See Also</h4><p class="seealso"><a href="#args">args</a>, <a href="#coalesce">coalesce</a>, <a href="#defaults">defaults</a></p>
</div>
<hr>

		<div class="navigation"><a href="index.html">toc</a></div>
	</body>
</html>
